/**
 * Created by yyj on 2015/10/14.
 */
$(function(){
    $('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    })
    //1 绑定删除按钮
    $('.add_btn').click(function(){
        layer.open({
        	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
            type:2,
            title:'添加码表页面',
            area:['400px','400px'],

            content:'initEditSysLookupCde.do?opera=add',
            maxmin: true,
            shadeClose: true
        });
    });
    //2 绑定删除按钮
    $('.delete_btn').click(function(){
        delete_func(this);
    });

    //3 绑定编辑按钮
    $('.edit_btn').click(function(){
        edit_func(this);
    });

    $("#name").focus();
    $(".resetBtn").click(function(){
        $("#cdeName").attr("value","");
        $("#cdeValue").attr("value","");
        $("#cdeFlag").attr("value","");
        $("#remark").attr("value","");
    });
})

edit_func=function(obj){
    var lookupcdeid=$(obj).parent().parent().attr('codeid');
    layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改码表页面',
        area:['400px','400px'],
        content:'initEditSysLookupCde.do?opera=edit&lookupCode='+lookupcdeid,
        maxmin: true,
        shadeClose: true
    });
}
delete_func=function(obj){
    var lookupcdeid=$(obj).parent().parent().attr('codeid');
    if(confirm('Are you sure you want to delete?')){
        $.ajax({
            url:"deleteLookupcde.do",
            type:"post",
            dataType: 'json',
            data:{
                "lookupCode":lookupcdeid
            },
            beforeSend:function(){
                $(obj).attr("disabled","disabled");
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.flag){
                    layer.msg('删除成功', {time:1300});
                    paging(1);
                }else{
                    layer.msg('删除失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
    }
}

//全选框选择
$(".CheckAll").change(function (){
	$(":checkbox[name='PageItem']").attr("checked",this.checked);
})

/**
 * 删除ci类型绑定的关系
 */
function deleteCiRelation(){
	if($("input[name='relationPageItem']:checked").length==0){
		layer.msg("请选择要删除的关系类型",{time:1500});
	}
	var ciRelationIds = "";
	$("input[name='relationPageItem']:checked").each(function(){
		ciRelationIds = ciRelationIds+$(this).val()+",";
	});
	ciRelationIds = ciRelationIds.substring(0,ciRelationIds.length-1);
	$.ajax({
		url:"deleteCiTypeRelation.do",
		type:"post",
		dataType: 'json',
		data:{
			"ciTypeId":$("#selectCiTypeId").val(),
			"ciTypeRelationIds":ciRelationIds
		},
		beforeSend:function(){
			$("#press_btn1").attr("disabled","disabled");
		},
		error:function(data){  
			layer.msg('出错了！！',{time:2000}); 
		},
		success:function(result){
			if(result.flag){
				layer.msg('删除成功', {time:1300});
				parent.pgReljump(15,1,'ci_relation_id desc');
				var index = parent.layer.getFrameIndex(window.name);
				setTimeout('parent.layer.close('+index+')',1310);
			}else{
				layer.msg('删除失败！', {time:1000});
				$("#press_btn1").removeAttr("disabled");
			}
		}
	});
}


//同步全选框
function manucheck(field){
	if(field.checked==false){
		$(".relationCheckAll").removeAttr("checked");
	}else{
		var flagCheckAll = true;
		$("input[name='relationPageItem']").each(function(){
			if($(this).attr("checked")!='checked'){
				flagCheckAll=false;
			} 
		});
		if(flagCheckAll){
			$(".relationCheckAll").attr("checked","checked");
		}
	}
}

function paging(pageNo) {
    var pageSize = $("#pageSize  option:selected").text();
    $.ajax({
        url: "pagingLookup.do",
        type: "post",
        dataType: 'json',
        data: {
            "iPageSize": pageSize,
            "iPageNo": pageNo
        },
        error: function (data) {
        	layer.msg('出错了！！',{time:2000});
        },
        success: function (jsons) {
            if (jsons != null) {
                $("#pageTable").html("");
                var pageList = jsons.pageData.pageList;
                for (var i = 0; i < pageList.length; i++) {
                    var partsForm =
                        '<tr codeid="'+pageList[i].lookupCode +'">' +
                        //'<td class="checkNot"><input type="checkbox" value="' + pageList[i].lookupCode + '" class="checkinput"/></td>' +
                        //  '<td class="checkNot"><input type="checkbox" onclick="manucheck(this);" value="'+pageList[i].ciTypeId+'" '+(pageList[i].canDeleteMsg!=''?'disabled':'name="pageItem"')+' class="checkinput"/></td>'+
                        // '<td class="checkNot"><input type="checkbox" name="checkradio"   value="'+pageList[i].lookupCode+'" class="checkinput" disabled="disabled"/></td>'+
                        '<td title=' + (pageList[i].lookupCode == null ? '' : pageList[i].lookupCode) + '>' + (pageList[i].lookupCode == null ? '' : pageList[i].lookupCode) + '</td>' +
                        '<td title=' + (pageList[i].lookupType == null ? '' : pageList[i].lookupType) + '>' + (pageList[i].lookupType == null ? '' : pageList[i].lookupType) + '</td>' +
                        '<td title=' + (pageList[i].cdeName == null ? '' : pageList[i].cdeName) + '>' + (pageList[i].cdeName == null ? '' : pageList[i].cdeName) + '</td>' +
                        '<td title=' + (pageList[i].cdeValue == null ? '' : pageList[i].cdeValue) + '>' + (pageList[i].cdeValue == null ? '' : pageList[i].cdeValue) + '</td>' +
//                        '<td title=' + (pageList[i].cdeFlag == null ? '' : pageList[i].cdeFlag) + '>' + (pageList[i].cdeFlag == null ? '' : pageList[i].cdeFlag) + '</td>' +
                        '<td title=' + (pageList[i].remark == null ? '' : pageList[i].remark) + '>' + (pageList[i].remark == null ? '' : pageList[i].remark) + '</td>' +
                        '<td><i class="Edit content_current edit_btn" title="编辑"></i><i class="Del content_current delete_btn" title="删除"></i></td>' +
                        '</tr>';
                    $(partsForm).appendTo("#pageTable");
                }
                //3 绑定编辑按钮
                $('.edit_btn').click(function(){
                    edit_func(this);
                });
                $('.delete_btn').click(function(){
                    delete_func(this);
                });

                //处理footer 分页
                var totalCount = jsons.pageData.totalCount;
                var pageSize = jsons.pageData.pageSize;
                var totalPage = jsons.pageData.totalPage;
                var pageNo = jsons.pageData.pageNo;
                dealFooterPage(totalCount, pageSize, totalPage, pageNo);
            }

        }
    });
}

function editLookupCode(lookupCodeId){
    layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改码表页面',
        area:['400px','400px'],
        content:'initEditSysLookupCde.do?opera=edit&lookupCode='+lookupCodeId,
        maxmin: true,
        shadeClose: true

    });
}
function reset_btn(){
    $("#cdeName").val('');
}
//同步全选框
  function manucheck(field){
  	if(field.checked==false){
  		$(".relationCheckAll").removeAttr("checked");
  	}else{
  		var flagCheckAll = true;
  		$("input[name='relationPageItem']").each(function(){
  			if($(this).attr("checked")!='checked'){
  				flagCheckAll=false;
  			} 
  		});
  		if(flagCheckAll){
  			$(".relationCheckAll").attr("checked","checked");
  		}
  	}
  }